package org.t236.order;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.t236.commons.model.AjaxResult;
import org.t236.commons.model.Order;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@Slf4j
public class OrderApp {
    @Value("${server.port}")
    private String port;

    private static List<Order> orders = new ArrayList<>();
    static {
        orders.add(new Order(1L, 1L, 200.0));
        orders.add(new Order(2L, 1L, 300.0));
        orders.add(new Order(3L, 2L, 400.0));
    }
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class, args);
    }

    @GetMapping("/orders")
    public AjaxResult find() {
        return AjaxResult.success(orders);
    }

    @GetMapping("/orders/findByUserId")
    public AjaxResult findByUserId(Long userId) {
        if (log.isDebugEnabled()) {
            log.debug("order-service server.port:{}", port);
        }
//        List<Order> lists = orders.stream().filter(o -> o.getUserId().equals(userId)).collect(Collectors.toList());
        List<Order> list = new ArrayList<>();
        for (Order order : orders) {
            if(order.getUserId().equals(userId)) {
                list.add(order);
            }
        }
        return AjaxResult.success(list);
    }
}
